package Service::LifeAssistantService;

use strict;
use warnings;
use 5.010;

use Exporter qw(import);

use Persistance::LifeAssistantDao;
use Native::DataCollector;
use Utils::Constants;
use GeoLocation::ReverseGeocoderFactory;

sub get_housework_for_user {
	my ($user) = @_;
	my @data = Persistance::LifeAssistantDao::get_housework_data($user);
	
	return @data;
}

sub get_specified_housework_detail {
	my ($housework_id) = @_;
	my @data = Persistance::LifeAssistantDao::get_specified_housework_detail($housework_id);

	return @data;
}

sub add_new_housework {
	my ($user_id,$task,$reward) = @_;
	my $result = Persistance::LifeAssistantDao::insert_new_housework($user_id,$task,$reward);

	return $result;
}

sub update_housework_status {
    my ($housework_id) = @_;
    my $result = Persistance::LifeAssistantDao::update_housework_status($housework_id);

    return $result;
}

sub get_bloodsugar_history_list {
	my ($user_id) = @_;
	my @data = Persistance::LifeAssistantDao::get_bloodsugar_history_list($user_id);

	return @data;
}

sub get_today_bloodsugar_stats {
    my ($user_id) = @_;
    my $resultCode = Native::DataCollector::generate_current_bloodsugar_linechart($user_id);

    return $resultCode == 1 ? Utils::Constants::BLOODSUGAR_STATS_IMAGE : '';
}

sub add_new_bloodsugar {
	my $result = Persistance::LifeAssistantDao::insert_new_bloodsugar(@_);

	return $result;
}

sub add_new_location {
	my $result = Persistance::LifeAssistantDao::insert_new_location_track(@_);

	return $result;
}

sub get_location_address_description {
    my ($is_living_in_china, $geolocation) = @_;
    my $map_name = (1 == $is_living_in_china) ? "BaiduMap" : "OpenStreetMap";
    my $reverse_geocoder = GeoLocation::ReverseGeocoderFactory->get_instance($map_name);

    return $reverse_geocoder->reverse_geocode($geolocation);
}

sub get_location_track_by_user {
	my ($user_id) = @_;
	my @data = Persistance::LifeAssistantDao::get_location_track_by_user($user_id);

	return @data;
}

sub get_all_users_location_today {
    my @data = Persistance::LifeAssistantDao::get_all_users_location_in_current_day();

    return @data;
}

sub save_contact_by_user {
    my ($user_id, $contacts) = @_;
    my $result = Persistance::LifeAssistantDao::insert_or_update_contacts($user_id, $contacts);

    return $result;
}

sub get_contacts_by_user {
    my $user_id = shift;
    my @data = Persistance::LifeAssistantDao::get_contacts_by_user_id($user_id);

    return @data;
}

1;
